package com.mjk.common.web.getway.restful.interceptor.type;


import com.mjk.common.base.response.BeeResponse;
import com.mjk.common.tools.interceptor.DefaultInterceptorChain;
import com.mjk.common.util.MapUtils;
import com.mjk.common.web.getway.constns.GetwayConsts;
import com.mjk.common.web.getway.restful.interceptor.GetWayInterceptorReq;
import com.mjk.common.web.getway.restful.interceptor.InterceptorInvoker;
import com.mjk.common.web.getway.restful.interceptor.type.commons.CommonInterceptorInvoker;
import com.mjk.common.web.getway.restful.interceptor.type.export.ExportInterceptorInvoker;
import com.mjk.common.web.getway.restful.interceptor.type.imports.ImportInterceptorInvoker;
import com.mjk.common.web.getway.restful.interceptor.type.pay.PayInterceptorInvoker;
import com.mjk.common.web.getway.restful.interceptor.type.view.ViewInterceptorInvoker;
import com.mjk.common.web.getway.restful.interceptor.type.websocket.WebSocketInterceptorInvoker;
import com.mjk.common.web.getway.restful.interceptor.type.ws.WsInterceptorInvoker;
import lombok.Getter;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 网关拦截器类型
 * @author mjk
 *
 */
public enum GetWayInterceptorType {
	ALL("all",null),
	/**
	 * 普通的请求
	 */
	COMMON("common",new CommonInterceptorInvoker()),
	/**
	 * 文件导入的请求
	 */
	IMPORT("import",new ImportInterceptorInvoker()),
	/**
	 * 文件导出的请求
	 */
	EXPORT("export",new ExportInterceptorInvoker()),
	/**
	 * 页面请求
	 */
	VIEW("view",new ViewInterceptorInvoker()),
	/**
	 * 支付
	 */
	PAY("pay",new PayInterceptorInvoker()),
	/**
	 * 医保
	 */
	MI("mi",new PayInterceptorInvoker()),
	/**
	 * webservice
	 */
	WS("ws",new WsInterceptorInvoker()),
	/**
	 * 
	 */
	SOAP("soap",new WsInterceptorInvoker()),
	/**
	 *
	 */
	MAGICAPI("magicapi",new WsInterceptorInvoker()),
	/**
	 * websocket
	 */
	WEBSOCKET("websocket",new WebSocketInterceptorInvoker())
	;
	@Getter
	private final String proto;
	
	private final InterceptorInvoker clazz;
	
	GetWayInterceptorType(String proto,InterceptorInvoker clazz){
		this.proto = proto;
		this.clazz = clazz;
	}


	/**
	 * 初始化拦截链
     */
	public static Map<String, DefaultInterceptorChain<GetWayInterceptorReq, BeeResponse>> getChains(){
		Map<String,DefaultInterceptorChain<GetWayInterceptorReq,BeeResponse>> chains =
                new ConcurrentHashMap<>();
		for (GetWayInterceptorType type : GetWayInterceptorType.values()) {			
				chains.put(type.getProto(), new DefaultInterceptorChain<>());
		}
		return chains;
	}
	/**
	 * 获取网关类型
     */
	public static InterceptorInvoker getGetWayInvoker(GetWayInterceptorReq req){
		String export = MapUtils.getString(req.getRestfulParams(), GetwayConsts.PROTOCOL);
		for (GetWayInterceptorType type : GetWayInterceptorType.values()) {
			if(export.equals(type.proto)){
				return type.clazz;
			}
		}
		return null;
	}
	
}